package com.sy.olddriver.dao.impl;

import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dao.UserDao;
import com.sy.olddriver.entity.User;
import com.sy.olddriver.utils.JdbcHelper;


/**
 * UserDao接口的实现类
 *
 * @author Administrator
 */
public class UserDaoImpl implements UserDao {

    @Override
    public User getUserByUserName(String userName) {
        return JdbcHelper.queryForBean("select * from user where userName=?", User.class, userName);
    }

    @Override
    public User getUserByPhone(String phone) {
        return JdbcHelper.queryForBean("select * from user where phone=?", User.class, phone);
    }

    @Override
    public boolean addUser(User user) {
        return JdbcHelper.update("insert into user(username,pwd,phone) values(?,?,?)", user.getUserName(), user.getPwd(), user.getPhone());
    }

    @Override
    public boolean updateUser(String content, String updateType, Integer userId) {
        StringBuilder sql = new StringBuilder();
        sql.append("update user set ");
        //更新名字
        if (AppConstant.UPDATE_TYPE_NAME.equals(updateType)) {
            sql.append("username=? ");
        }
        //更新手机号
        if (AppConstant.UPDATE_TYPE_PHONE.equals(updateType)) {
            sql.append("phone=? ");
        }

        //TODO:其它的情况
        sql.append("where id=?");
        return JdbcHelper.update(sql.toString(), content, userId);
    }

    @Override
    public User getUserById(Integer userId) {
        return JdbcHelper.queryForBean("select * from user where id=?", User.class, userId);
    }
}
